using UnityEngine;
using System.Collections;

[System.Serializable]
public class BatteryLevelFlashlight : MonoBehaviour {
	
	[System.Serializable]
	public class FlashlightAnimation {
		
		[System.Serializable]
		public class FlashLightButton {			
			public GameObject button;
			public string animationOnPress;
			public string animationIdle;
		}
		
		[System.Serializable]
		public class HandOnFlashlightButton {
			public GameObject hand;
			public string animationOnPress;
			public string animationIdle;
		}
		
		public FlashLightButton flashlightButton = new FlashLightButton();
		public HandOnFlashlightButton handOnFlashlightButton = new HandOnFlashlightButton();
	}
	
	[System.Serializable]
	public class Animations {
		public GameObject characterToAnimate;
		public string idle = "";
		public string walk = "";
		public string run = "";
		
		public GameObject[] objectsToStopAnimatinglWhenPressingR;
	}
	
	public FlashlightAnimation flashlightAnimation = new FlashlightAnimation();
	public Animations animations = new Animations();
	
	public Light flashlight;
	public Color lowBattery;
	public bool batteryPickedUp = false;	
	public float batteryLevel = 500.0f;
	public float turnOnFlashlightAfterBatteryChangeSeconds = 3.0f;
	public Texture batteryLevelHighTexture;
	public Texture batteryLevelLowTexture;
	
	public CharacterMotor characterMotor;	
	
	private float time = 0.0f;
	private float startTime = 0.0f;
	private float delta = 0.0f;
	private float batteryLevelPercent = 100;
	private float maxBatteryLevel = 0.0f;
	private Texture batteryLevelTexture;
	
	private float minSpotAngle = 37.5024f;
	private float minIntensity = 0.7f;
	private float minRange = 95.0f;
	private float maxSpotAngle = 56.7f;
	private float maxIntensity = 1.6f;
	private float maxRange = 125.0f;
	
	public bool wasFlashlightOnWhenBatteryChanged = true;
	
	// Use this for initialization
	void Start () {
		maxBatteryLevel = Game.game.getFlashlightMaxPower();
		if (animations.characterToAnimate != null) {
			animations.characterToAnimate.animation.Play(animations.idle);
			animations.characterToAnimate.animation.Blend(flashlightAnimation.handOnFlashlightButton.animationIdle);
		}
	}
	
	// Update is called once per frame
	void Update () {		
		if (animations.characterToAnimate != null) {
			if (Input.GetKey(KeyCode.W)) {
				//animations.characterToAnimate.animation.Stop(flashlightAnimation.handOnFlashlightButton.animationIdle);				
				if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) {
					characterMotor.movement.maxBackwardsSpeed = 7;
					characterMotor.movement.maxForwardSpeed = 7;
					characterMotor.movement.maxSidewaysSpeed = 7;
					
					animations.characterToAnimate.animation.Stop(animations.idle);
					animations.characterToAnimate.animation.Stop(animations.walk);
					
					animations.characterToAnimate.animation.Blend(animations.run);
					
				} else {
					characterMotor.movement.maxBackwardsSpeed = 3;
					characterMotor.movement.maxForwardSpeed = 3;
					characterMotor.movement.maxSidewaysSpeed = 3;
					
					animations.characterToAnimate.animation.Stop(animations.idle);
					animations.characterToAnimate.animation.Stop(animations.run);
					
					animations.characterToAnimate.animation.Blend(animations.walk);
					
				}
			} else {
				animations.characterToAnimate.animation.Stop(animations.run);
				animations.characterToAnimate.animation.Stop(animations.walk);
				
				animations.characterToAnimate.animation.Blend(animations.idle);
				
			}
			animations.characterToAnimate.animation.Blend(flashlightAnimation.handOnFlashlightButton.animationIdle);
		}
		
		if (batteryPickedUp) {
			if (startTime <= 0.0f) {
				startTime = Time.time;	
			}
			time = Time.time-startTime;
		}
		if (time >= turnOnFlashlightAfterBatteryChangeSeconds && batteryPickedUp) {
			flashlight.enabled = wasFlashlightOnWhenBatteryChanged;
			batteryPickedUp = false;
		}
		
		if (Input.GetKeyUp(KeyCode.F) || Input.GetMouseButtonUp(2)) {
			flashlight.enabled = !flashlight.enabled;
			if (flashlightAnimation.flashlightButton.button != null) {
				flashlightAnimation.handOnFlashlightButton.hand.animation.Stop(flashlightAnimation.handOnFlashlightButton.animationIdle);
				if (flashlightAnimation.flashlightButton.animationOnPress != null) {
					flashlightAnimation.flashlightButton.button.animation.Blend(flashlightAnimation.flashlightButton.animationOnPress);
				}
				if (flashlightAnimation.handOnFlashlightButton.animationOnPress != null) {
					flashlightAnimation.handOnFlashlightButton.hand.animation.Blend(flashlightAnimation.handOnFlashlightButton.animationOnPress);
				}
				flashlightAnimation.flashlightButton.button.audio.Play();
			}
		} 		
		if (Input.GetAxis("Mouse ScrollWheel") > 0 
				&& flashlight.spotAngle < maxSpotAngle 
				&& flashlight.intensity < maxIntensity 
				&& flashlight.range < maxRange) {
			light.spotAngle *= 1.2f;
			light.intensity *= 1.2f;	
			light.range *= 1.2f;
		} else if (Input.GetAxis("Mouse ScrollWheel") < 0 
				&& flashlight.spotAngle > minSpotAngle 
				&& flashlight.intensity > minIntensity 
				&& flashlight.range > minRange) {
			light.spotAngle /= 1.2f;
			light.intensity /= 1.2f;
			light.range /= 1.2f;
		}
	}
	
	void FixedUpdate() {
		if (flashlight.enabled) {
			batteryLevelPercent = (batteryLevel / maxBatteryLevel) * 100;
			if (batteryLevelPercent > 100)
				batteryLevelPercent = 100;
			if (!batteryPickedUp || flashlight.enabled) {
				if (batteryLevelPercent < 30 && batteryLevelPercent > 0) {
					flashlight.color = lowBattery;	
				} else if (batteryLevelPercent > 30) {
					flashlight.color = Color.white;	
				}
				if (batteryLevel > 0) {
					// Easy battery capacity: 1800 seconds
					// 1800 = 100% batteryLevel
					delta = Time.fixedDeltaTime;
					batteryLevel -= delta;
				} else {
					batteryLevel = 0;
					flashlight.enabled = false;
				}
			} else {
				
			}
		}
	}
	
	void OnGUI() {
		//GUI.Box(new Rect(1, 2, 200, 64), "Battery: " + batteryLevelPercent);
		string remainingTime = "";
		
		float h = 0;
		int iH = 0;
		
		float m = 0;
		int iM = 0;
		
		float s = 0;
		int iS = 0;
		
		float rest = batteryLevel;
		
		if (batteryLevel >= 3600) {
			h = batteryLevel / 3600;
			iH = (int) h;
			
			rest -= 3600 * iH;
			
			m = rest / 60;
			iM = (int) m;
			
			rest -= 60 * iM;
			
			s = rest;
			iS = (int) s;
		} else if (batteryLevel >= 60) {
			m = batteryLevel / 60;
			iM = (int) m;
			
			rest -= 60 * iM;
			
			s = rest;
			iS = (int) s;
		} else if (batteryLevel < 60) {
			s = batteryLevel;
			iS = (int) s;
			
			rest -= iS;
		}
		if (batteryLevelPercent < 30 && batteryLevelPercent > 0) {
			batteryLevelTexture = batteryLevelLowTexture;
		} else if (batteryLevelPercent >= 30) {
			batteryLevelTexture = batteryLevelHighTexture;	
		}
		GUI.DrawTexture(new Rect(16,Screen.height-32, (Screen.width*(batteryLevelPercent/100)*0.3f)+16, 20), batteryLevelTexture);
		//GUI.Label(new Rect(20, Screen.height-32, ((Screen.width*100)*0.3f)+16, 20), "" + iH + "h" + iM + "m" + iS + "s");
		GUI.Label(new Rect(20, Screen.height-32, ((Screen.width*100)*0.3f)+16, 20), "" + ((int) batteryLevelPercent) + "%");
		//Debug.Log("Rest: " + rest);
	}
}
